package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.utils.CommonMessage;
import com.etc.utils.PageData;

@Controller
@RequestMapping(value = "adminapi")
public class AMerchantController {

	Logger logger = Logger.getLogger(AUserController.class);

	// 注入一个UserService对象
	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "userMapper")
	private UserMapper UserMapper;

	/**
	 * 显示商家列表的方法
	 * 
	 * @param page
	 * @param pageSize
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "listMerchant", method = RequestMethod.GET)
	public String listUser(Integer page, Integer pageSize, String content, Integer ustate, Model model) {
		if (content == null) {
			content = "";
		}

		PageData<User> pd = userService.getUserByPage(1, 10, content, 2);
		List<User> list = pd.getData();

		model.addAttribute("list", list);

		return "admin/merchant-list";

	}

	/**
	 * 添加商家步骤一
	 * 
	 * @return
	 */
	@RequestMapping(value = "intoMerchantAdd")
	public String intoMenber() {
		return "admin/merchant-add";
	}

	/**
	 * 添加商家步骤二
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "addMerchant", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage addUser(@RequestBody User user) {

		logger.info("addMerchant************:" + user);

		boolean flag = userService.addUser(user);
		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

	/**
	 * 修改商家步骤一
	 */
	@RequestMapping(value = "updateMerchant01", method = RequestMethod.GET)
	public String updateUser01(int uid, String uname, String upwd, String usex, String utel, int ustate, Model model) {
		logger.info("updateMerchant01************:" + uid + uname + upwd + usex + utel + ustate);

		User user = new User(uid, uname, upwd, usex, utel, ustate);

		model.addAttribute("user", user);
		return "admin/merchant-update";
	}

	/**
	 * 修改商家步骤二
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "updateMerchant", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage updateUser(@RequestBody User user) {

		logger.info("updateMerchant************:" + user);

		boolean flag = userService.updateUser(user);

		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

	/**
	 * 删除商家
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping(value = "deleteMerchant", method = RequestMethod.GET)
	@ResponseBody
	public CommonMessage deleteUser(int uid) {
		logger.info("updateMerchant01************:" + uid);

		boolean flag = userService.deleteUserById(uid, 0);

		CommonMessage cm = new CommonMessage();
		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;

	}

}
